#include <stdio.h>

#if 0
int bitcount(unsigned int n) {
	/* works for 32-bit numbers only    */
	/* fix last line for 64-bit numbers */
	register unsigned int tmp;
	printf("%#x\n", ((n >> 1) & 033333333333));
	printf("%#x\n", ((n >> 2) & 011111111111));

	tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
	printf("%#o\n", tmp);

	return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}
#endif

int bitcount(unsigned int x )
{
	static unsigned int mask[] = { 0x55555555,
		0x33333333,
		0x0F0F0F0F,
		0x00FF00FF,
		0x0000FFFF
	} ;

	int i ;
	int shift ; /* Number of positions to shift to right*/
	for ( i =0, shift =1; i < 5; i ++, shift *= 2) {
		printf("xmask : %#x\n", x & mask[i]);
		printf("xsmask: %#x\n", (x >> shift) & mask[i]);

		x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
		printf("x     : %#x\n", x);
	}
	return x;
}
int main()
{
	printf("%d\n", bitcount(25));
	return 0;
}

